package com.chen.service.impl;

import com.chen.dao.OrderSettingDao;
import com.chen.exception.MyException;
import com.chen.pojo.OrderSetting;
import com.chen.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @Author zhebie
 * @DATE 2021/9/19
 */
@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 批量导入预约设置
     * @param osList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doImport(List<OrderSetting> osList) {
        //非空判断
        if (!CollectionUtils.isEmpty(osList)){
            //遍历
            for (OrderSetting orderSetting : osList) {
                //获取导入的预约设置信息
                Date orderDate = orderSetting.getOrderDate();
                //通过日期查询预约设置表
                OrderSetting osInDB = orderSettingDao.findByOrderDate(orderDate);
                //如果存在记录
                if (null != osInDB){
                    //获取数据库中的已预约人数
                    int reservations = osInDB.getReservations();
                    //取出要跟新的最大预约数
                    int number = orderSetting.getNumber();
                    //如果已经预约人数>要更新的最大预约数，报错
                    if (reservations > number){
                        throw new MyException("最大预约数不能小于已经预约的人数");
                    }
                    //更新最大预约数
                    orderSettingDao.updateNumber(orderSetting);
                }else{
                    //如果不存在，添加新记录
                    orderSettingDao.add(orderSetting);
                }
            }
        }
        //事务控制
    }

    //根据日期得到这个月每一天的预约
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //组织查询Map,dateBegin表示月份开始时间，dateEnd月份结束时间
        String dateBegin = date + "-1";
        String dateEnd = date + "-30";
        HashMap<Object,Object> map = new HashMap<>();
        map.put("dateBegin",dateBegin);//name  2021-09-01
        map.put("dateEnd",dateEnd);
        //查询当前月份的预约设置
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        //将List<OrderSetting>,组织成List<Map>
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());//获得日期
            orderSettingMap.put("number",orderSetting.getNumber());//可预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());//已预约人数
            data.add(orderSettingMap);
        }
        return data;

    }

    //预约设置修改可预约人数
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count>0){
            //当前日期已经进行了预约设置，需要进行修改操作
            orderSettingDao.editNumberByDate(orderSetting);
        }else{
            //当前日期没有进行预约设置，进行添加操作
            orderSettingDao.add(orderSetting);
        }
    }
}
